package com.zkingsoft.services.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.BabyDao;
import com.zkingsoft.dao.FlowDao;
import com.zkingsoft.dao.YkDao;
import com.zkingsoft.model.Baby;
import com.zkingsoft.model.Flow;
import com.zkingsoft.model.Yk;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.services.BabyService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-08 21:00
 */
@Service("babyService")
public class BabyServiceImpl implements BabyService {

	@Autowired
	private BabyDao babyDao;
	
	@Autowired
	private YkDao ykDao;
	
	
	@Autowired
	private FlowDao flowDao;
	
	

	@Override
	public int add(Baby baby) {

		return babyDao.insert(baby);

	}

	@Override
	public int modify(Baby baby) {

		return babyDao.update(baby);

	}

	@Override
	public int remove(List<Long> list) {

		return babyDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long id) {

		return babyDao.deleteById(id);

	}

	@Override
	public int removeByModel(Baby baby) {

		return babyDao.deleteByModel(baby);

	}

	@Override
	public List<Baby> findInPage(Baby baby, PaginationVO pageVo) {

		return babyDao.selectInPage(baby, pageVo);

	}

	@Override
	public List<Baby> findByModel(Baby baby) {

		return babyDao.selectByModel(baby);

	}

	@Override
	public int findTotal(Baby baby) {

		return babyDao.selectTotalRecord(baby);

	}

	@Override
	public Baby findById(Long id) {

		return babyDao.selectById(id);

	}

	@Transactional
	@Override
	public int addDf(Long long1, Yk yk, HttpServletRequest request) {
		int i = 0;
		Baby baby = babyDao.selectById(long1);
		baby.setDf(baby.getDf() + 1);
		//改变baby
		i = babyDao.update(baby);
		Date date=new Date(); 
		//改变游客
		yk.setBabyId(long1);
		yk.setIp(request.getRemoteAddr());
		yk.setTpsj(date);
		ykDao.update(yk);
		
		//新增流水
		Flow flow=new Flow();
		flow.setBabyId(long1);
		flow.setCreateTime(date);
		flow.setIp(request.getRemoteAddr());
		flow.setUrl(request.getRequestURI());
		flow.setYkId(yk.getId());
		flowDao.insert(flow);
		return i;
	}

	@Override
	public List<Baby> findByKeyWord(String keyword) {
		return babyDao.selectByKeyWord(keyword);
	}
	
}